package com.flower8.service.impl;

import com.flower8.dao.RedbagDao;
import com.flower8.dao.UserDao;
import com.flower8.service.RedbagService;
import com.flower8.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: tanglin
 * @Date: 2019/11/14 19:16
 * @Version: 1.0
 */
@Service
public class RedbagServiceImpl implements RedbagService {

    @Autowired
    private RedbagDao redbagDao;
    @Autowired
    private UserDao userDao;

    /**
     * 扫描过期红包
     * @throws Exception
     */
    @Transactional
    public void updateExpireRedbag()throws Exception{
        Map param = new HashMap<>();
        String today = DateUtils.formatDate(new Date());
        param.put("today", today);
        System.out.println(today+"++++++++++ 开始扫描用户过期红包并更新 ++++++++++");
        //获取当天要过期红包的用户和金额
        List<Map> expireList = redbagDao.getExpireRedbagList(param);
        for (Map expire : expireList) {
            Map updParam = new HashMap<>();
            updParam.put("uid", expire.get("uid"));
            updParam.put("money",expire.get("expireMoney"));
            //添加一条红包过期记录
            redbagDao.addRedbagRecord(updParam);
            //更新用户钱包
            userDao.updateUserBag(updParam);
        }
        //将获取红包状态置为过期
        redbagDao.updateExpireRedbag(param);
        System.out.println(today+"++++++++++ 已完成扫描用户过期红包并更新 ++++++++++");

    }

    /**
     * 查询红包、大转盘商品
     * @param flag 1 :红包  2：大转盘
     * @return
     * @throws Exception
     */
    @Override
    public List<Map> getGoodsList(long flag) throws Exception {
        Map param  = new HashMap();
        param.put("flag",flag);
        return redbagDao.getGoodsList(param);
    }
}
